<template>
	<view class="content">
		<image class="image_icon"></image>
		<view class="cu-load" :class="!isLoad?'loading':'over'">{{$t('loading')}}...</view>
	</view>
</template>

<script>
	// var wx = require('components/node_modules/jweixin-module');
	var _self = null
	export default {
		data() {
			return {
				isLoad: false
			}
		},
		onLoad() {
			_self = this
			console.log('splash onLoad');
			//动态设置Tabbar的文字
			if (getApp().globalData.version == this.$constant.PROJECT_YINNI) {
				this.go_padLogin()
			} else {
				if (getApp().globalData.debug) {
					uni.setStorage({
						key: 'openid',
						data: 'oFxXswJ6XZnPwHOvfIVbp3BkSc4w',
						success: function() {
							console.log('success');
						}
					});
				}
				//判断本地是否已缓存openid
				uni.getStorage({
					key: 'openid',
					success: function(res) {
						// console.log(res);
						if (res.data != '') {
							_self.checkUserBindding(res.data)
						} else {
							_self.getOpenId()
						}
					},
					fail: function(res) {
						// console.log('fill = ', res);
						_self.getOpenId()
					}
				});
			}
			//初始化wxJssdk
			// _self.initWxSdk()
		},
		methods: {
			//账号密码登陆
			go_padLogin(){
				uni.redirectTo({
					url: '../login/pad-login'
				})
			},
			go_login() {
				uni.redirectTo({
					url: '../login/login'
				})
			},
			go_index() {
				uni.switchTab({
					url: '../index/index'
				});
			},
			go_company(company) {
				uni.redirectTo({
					url: `../selectcompany/selectcompany?company=${JSON.stringify(company)}`
				})
			},
			getOpenId() {
				//请求获取openId
				uni.request({
					url: _self.$constant.BASEURL + _self.$constant.GET_OPENID,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						code: getApp().globalData.sceneId
					},
					success: (res) => {
						//跳转到登陆页面
						if (res.data.code == 0) {
							var openid = res.data.data
							// console.log('openid', openid)
							//保存openid
							uni.setStorage({
								key: 'openid',
								data: res.data.data,
								success: function() {
									// console.log('success111');
								}
							});
							//判断用户是否已被绑定
							_self.checkUserBindding(openid)
						} else {
							uni.showModal({
								title: "提示",
								content: "您需要在同意授权微信登录之后，才能继续使用",
								showCancel: false,
								success() {
									//关闭当前网页  顺序不能颠倒 同时兼容ios和android
									document.addEventListener('WeixinJSBridgeReady', function() {
										WeixinJSBridge.call('closeWindow');
									}, false);
									WeixinJSBridge.call('closeWindow');
								}
							})
						}
					}
				});
			},
			//初始化微信JSSDK
			checkUserBindding(openid) {
				//保存openid到全局
				getApp().globalData.openid = openid
				uni.request({
					url: _self.$constant.BASEURL + _self.$constant.CHECK_USER_BIND,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						openid: openid
					},
					success: (res) => {
						console.log(res)
						var result = res.data
						console.log(result)
						switch (result.code) {
							//账号已绑定，但未选择公司
							case '0':
								_self.go_company(result.data)
								break;
								//账号已绑定，且已经选择公司
							case '1':
								// console.log(result)
								getApp().globalData.unitno = result.data.departlist[0].unitno
								getApp().globalData.userid = result.data.departlist[0].userid
								this.$constant.saveUserid(result.data.departlist[0].userid)
								this.$constant.saveUnitno(result.data.departlist[0].unitno)
								_self.go_index()
								break;
								//未绑定
							default:
								_self.go_login()
								break;
						}
					},
					fail() {
						uni.showToast({
							title: '网络错误',
							icon: 'none'
						})
					}
				});
			}
		}
	}
</script>

<style>
	.image_icon {
		width: 200upx;
		height: 200upx;
		margin-top: 400upx;
	}

	.cu-load.loading::after {
		content: "";
	}
</style>
